package com.interesting.client.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.interesting.R;
import com.interesting.base.utils.Constant;
import com.interesting.client.UI.commonActivity.BrowserBigPicActivity;
import com.interesting.client.UI.commonActivity.UIUtil;
import com.interesting.client.application.AppHolder;
import com.interesting.client.bean.Message;
import com.interesting.client.widget.RadioWithTitle;
import com.interesting.client.widget.RadioWithTitle.RadioEventListener;

/**
 * 醉新 醉热门列表adapter
 **/
public class MessageListAdapter extends BaseAdapter {

	private List<Message> list;
	private Context context;
	private LayoutInflater mlinflater;

	private class ViewHolder {
		public ImageView icon, photo, share, collect;
		public TextView name, content;
		public RadioWithTitle radio;
	}

	public MessageListAdapter(Context context, List<Message> data) {
		this.context = context;
		this.list = data;
		this.mlinflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		if (position < list.size()) {
			return list.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final Message mesg = list.get(position);
		ViewHolder vh = null;
		if (convertView == null) {
			convertView = mlinflater.inflate(R.layout.message_list_item_adapter, null);
			vh = new ViewHolder();

			vh.icon = (ImageView) convertView.findViewById(R.id.message_list_item_adapter_icon);
			vh.name = (TextView) convertView.findViewById(R.id.message_list_item_adapter_username);
			vh.content = (TextView) convertView.findViewById(R.id.message_list_item_adapter_content);
			vh.photo = (ImageView) convertView.findViewById(R.id.message_list_item_adapter_photo);
			vh.collect = (ImageView) convertView.findViewById(R.id.message_list_item_adapter_fav);
			vh.share = (ImageView) convertView.findViewById(R.id.message_list_item_adapter_share);
			vh.radio = (RadioWithTitle) convertView.findViewById(R.id.message_list_item_adapter_radio);
			convertView.setTag(vh);

		} else {
			vh = (ViewHolder) convertView.getTag();
			resetViewHolder(vh);
		}

		convertView.setTag(vh);

		AppHolder.getInsatnce().imageLoader.DisplayImage(Constant.IMGPATH + "/" + mesg.getAuthorIcon(), vh.icon,
				R.drawable.ic_user_default);
		if (mesg.getMinImgPath() != null && !mesg.getMinImgPath().equals("")) {
			vh.photo.setVisibility(View.VISIBLE);
			AppHolder.getInsatnce().imageLoader.DisplayImage(Constant.IMGPATH + "/" + mesg.getMinImgPath(), vh.photo,
					R.drawable.ic_user_default);
		}
		vh.photo.setTag(Constant.IMGPATH + "/" + mesg.getImgPath());// 大图地址
		vh.radio.setStatus(mesg.getStatus());
		vh.radio.setGood(mesg.getNtop());
		vh.radio.setBad(mesg.getNdown());
		vh.name.setText(mesg.getAuthor());
		vh.content.setText(mesg.getContent());
		vh.collect.setTag(mesg);
		if (mesg.getStatus() == 1) {
			vh.radio.setTopCheck(true);
			vh.radio.setDownCheck(false);
		}
		if (mesg.getStatus() == -1) {
			vh.radio.setDownCheck(true);
			vh.radio.setTopCheck(false);
		}
		vh.radio.setRadioEvent(new RadioEventListener() {

			@Override
			public void event(int status) {
				// 执行方法
				if (status == 1) {
					new UIUtil.TopOrDownAsync(context).execute(mesg.getId(), "1");
					mesg.setNtop(mesg.getNtop() + 1);

					if (mesg.getStatus() != 0)
						mesg.setNdown(mesg.getNdown() + 1);

				} else if (status == -1) {
					new UIUtil.TopOrDownAsync(context).execute(mesg.getId(), "-1");
					if (mesg.getStatus() != 0)
						mesg.setNtop(mesg.getNtop() - 1);
					mesg.setNdown(mesg.getNdown() - 1);
				}

				mesg.setStatus(status);

			}
		});
		vh.photo.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 照片点击事件
				Intent intent = new Intent(context, BrowserBigPicActivity.class);
				intent.putExtra("msg", v.getTag().toString());
				context.startActivity(intent);
			}
		});
		vh.collect.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 收藏点击事件
				if (UIUtil.isLoggedIn()) {
					String userId = AppHolder.getInsatnce().user.getUserId();
					Message mesg = (Message) v.getTag();
					new UIUtil.FavouriteTask(context).execute(userId, mesg.getId(), "0");
				} else {
					UIUtil.showToast(context, "请先登录 在收藏");
				}
			}
		});
		vh.share.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 分享
				LinearLayout vh = (LinearLayout) v.getParent().getParent();
				TextView text = (TextView) vh.findViewWithTag("content");
				Intent intentshare = new Intent(Intent.ACTION_SEND);
				intentshare.setType("text/plain");
				intentshare.putExtra(Intent.EXTRA_SUBJECT, "来自最有趣");
				intentshare.putExtra(Intent.EXTRA_TEXT, text.getText().toString());
				context.startActivity(Intent.createChooser(intentshare, "醉有趣分享"));
			}
		});
		return convertView;
	}

	protected void resetViewHolder(ViewHolder vh) {
		vh.radio.setStatus(0);
		vh.name.setText("");
		vh.content.setText("");
		vh.photo.setVisibility(View.GONE);

	}

}
